package com.ffcs.crmd.tsp.server.netty.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobGroup;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobGroup;

import io.netty.channel.ChannelHandlerContext;

/**
 * 
 * 功能说明:删除任务执行者
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年1月21日 上午9:44:55
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class DeleteJobGroupProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 数据源控制中心
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public DeleteJobGroupProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.info("SERVER-NETTY: one request to delete a job group is received that body=[{}]", requestXml);
            TspJobGroup tspJobGroupVO = (TspJobGroup) XmlUtil.fromXml(requestXml, TspJobGroup.class);
            JobGroup jobGroup = new JobGroup();
            BeanUtils.applyIf(jobGroup, tspJobGroupVO);
            dataBaseHandler.getServiceSupport().deleteJobGroup(jobGroup);
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: delete a job group[{}] exception:", requestXml, e);
            String responseBody = String.format("delete job group exception:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
